use diesel::prelude::*;
use diesel::MysqlConnection;

use super::model::*;
use crate::database::{crud, schema::menu, DbError};
use crate::{db_create, db_delete, db_read_count_from_offset, db_update};

db_create!(menu, Menu, NewMenu, create);
db_read_count_from_offset!(menu, Menu, read_count_from_offset);
db_update!(menu, NewMenu, update);
db_delete!(menu, delete);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        database::establish_connection, db_read, test_db_create, test_db_delete, test_db_update,
    };
    use diesel::Connection;

    db_read!(menu, Menu, read);

    fn insert_test_data(conn: &mut MysqlConnection) -> (i32, i32, i32) {
        let mid1 = create(
            conn,
            NewMenu {
                name: "MENU1",
                icon: "ICON1",
                link: "LINK1",
                visibility_level: 1,
            },
        )
        .unwrap();
        let mid2 = create(
            conn,
            NewMenu {
                name: "MENU2",
                icon: "ICON2",
                link: "LINK2",
                visibility_level: 1,
            },
        )
        .unwrap();
        (mid1, mid2, 0)
    }

    test_db_create!();
    test_db_update!(name, "NEW_MENU".to_string());
    test_db_delete!();
}
